package com.tc.nio;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.channels.FileChannel;

/**
 * @Auther: tianchao
 * @Date: 2022/1/30 10:54
 * @Description:
 */
@Slf4j
public class TestFileChannelTransferTo {
    public static void main(String[] args) throws FileNotFoundException {
        System.out.println(System.getProperty("user.dir"));
        try (
//            FileChannel from = new FileInputStream("originTrans.txt").getChannel();
//            FileChannel to = new FileOutputStream("originTransTarget.txt").getChannel();
            FileChannel from = new FileInputStream("C:\\Users\\DELL\\Downloads\\ideaIU-2019.3.exe").getChannel();
            FileChannel to = new FileOutputStream("idea.exe").getChannel();
        ){
            //文件拷贝 效率高 以后分析为啥快 底层会利用操作系统的零拷贝进行优化
            //from.transferTo(0,from.size(),to);
            long size = from.size();
            for (long left = size;left>0;){
                log.debug("position: {}, left: {}",size - left, left);
                left -= from.transferTo(size-left, left, to);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
